#include "main.h"

//Camera 1

void moveMeFlat(int direction) 
{
	//Check the future position to be inside the sky box, if not stop the movement
	if(fabs(x + direction*(lx)*0.25)<45 && fabs(y + direction*(ly)*0.25)<45 && fabs(z + direction*(lz)*0.25)<45)
	{
	x = x + direction*(lx)*0.2;  //update x position
	y = y + direction*(ly)*0.2;  //update y position
	z = z + direction*(lz)*0.2;  //update z position
	glLoadIdentity();
	}
}
void orientMeX(float angx,float angy)
{
	lx = sin(angx);  //update viewing point on the x axis
	lz = -cos(angx); //update viewing point on the z axis
	glLoadIdentity();
}
void orientMeY(float angx,float angy) 
{
	ly = sin(angy); //update viewing point on the x axis
	lz = -cos(angx); //update viewing point on the z axis
	glLoadIdentity();
}
void setCamera1()
{
	//Set the camera position
	gluLookAt(x,y,z, 
		  x+lx,y+ly,z+lz,
		  0.0, 1.0, 0.0);
}

//Camera 2
void moveLeft()
{
	if(xp>=radius || xp<=-radius){ speedL=speedR;
	                             speedR=-speedR;
								 side=-side;
	                            }
	xp+=speedL;
	if(xp>=radius) xp=radius;
	else if(xp<=-radius) xp=-radius;
	zp=side*sqrt(pow(radius,2)-pow(xp,2));
}

void moveRight()
{
	
	if(xp>=radius || xp<=-radius){ speedR=speedL;
	                             speedL=-speedL;
								 side=-side;
	                            }
	xp+=speedR;
	if(xp>=radius) xp=radius;
	else if(xp<=-radius) xp=-radius;
    zp=side*sqrt(pow(radius,2)-pow(xp,2));
}

void moveUp(float speed)
{
	yp+=speed;
}

void zoom(float size)
{
	if(xp*(radius+size)/radius<43 && zp*(radius+size)/radius<44)
	{
	xp=xp*(radius+size)/radius;
	zp=zp*(radius+size)/radius;
	radius+=size;
	}
}

void setCamera2()
{
	//set the camera
	gluLookAt(xp,yp,zp,
	      0,0,0,
		  0.0,1.0,0.0);
}


